package com.github.ecbp.store.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.req.BaseIdStatusDTO;
import com.github.ecbp.common.utils.Asserts;
import com.github.ecbp.store.service.common.StoreCodeAndMessageEnum;
import com.github.ecbp.store.service.dto.ProductDTO;
import com.github.ecbp.store.service.dto.ProductQueryDTO;
import com.github.ecbp.store.service.service.ProductService;
import com.github.ecbp.store.service.vo.ProductPageVO;
import com.github.ecbp.store.service.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * @Author Created by Alex·He on 2020/4/15 17:23
 * @Version 1.0
 * @Description 商品表(Product)表控制层
 */
@Api(tags = "商品信息管理")
@RequestMapping("/api/" + PROJECT_NAME + "/product")
@RestController
public class ProductController extends BaseController {
    @Autowired
    private ProductService service;

    @ApiOperation(value = "添加商品")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody ProductDTO param) {
        service.save(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID删除商品")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        Asserts.isTrue(service.removeById(param.getId()), StoreCodeAndMessageEnum.PRODUCT_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "批量删除商品")
    @PostMapping("/deleteByBatch")
    public CommonResult deleteByBatch(@Validated @RequestBody BaseIdListDTO param) {
        Asserts.isTrue(service.removeByIds(param.getIds()), StoreCodeAndMessageEnum.PRODUCT_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改商品")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody ProductDTO param) {
        return CommonResult.succeed(service.updateById(param));
    }

    @ApiOperation(value = "修改商品的上架状态")
    @PostMapping(value = "/updatePublishStatus")
    public CommonResult updatePublishStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updatePublishStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "修改商品的新品状态")
    @PostMapping(value = "/updateNewStatus")
    public CommonResult updateNewStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updateNewStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "修改商品的人气推荐状态")
    @PostMapping(value = "/updateRecommandStatus")
    public CommonResult updateRecommandStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updateRecommandStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "修改商品的预告商品状态")
    @PostMapping(value = "/updatePreviewStatus")
    public CommonResult updatePreviewStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updatePreviewStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "根据ID获取商品详情", response = ProductVO.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }

    @ApiOperation(value = "分页列表-商品", response = ProductPageVO.class)
    @PostMapping(value = "/listAllPage")
    public CommonResult listAllPage(@Validated @RequestBody ProductQueryDTO param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(param), ProductPageVO.class));
    }
}
